package com.stackandqueue;

import java.util.LinkedList;

public class Solution06 {
    public int evalRPN(String[] tokens) {
        LinkedList<Integer> deque = new LinkedList<>();
        for (int i = 0; i < tokens.length; i++) {
            if ("+".equals(tokens[i])) {
                deque.add(deque.removeLast() + deque.removeLast());
            } else if ("-".equals(tokens[i])) {
                deque.add(-deque.removeLast() + deque.removeLast());
            } else if ("*".equals(tokens[i])) {
                deque.add(deque.removeLast() * deque.removeLast());
            } else if ("/".equals(tokens[i])) {
                Integer tmp1 = deque.removeLast();
                Integer tmp2 = deque.removeLast();
                deque.add(tmp2/tmp1);
            } else deque.add(Integer.valueOf(tokens[i]));
        }
        return deque.removeLast();
    }
}
